# -*- coding: utf-8 -*-  
from extern import *
import json
import time
if  __name__=='__main__':
    start_day='20200101'
    pro = getpro()
    con,db=mongocon()
    collist=db.list_collection_names()
    is_exists=False
    tbname='block_trade'
    if 'block_trade' in collist:
        print('table is exists.')
        is_exists=True
    mftb = db['block_trade']
    if is_exists==False:
        df = pro.trade_cal(exchange='', start_date=start_day, end_date=latestday(),is_open=1)
        tradedays=list(df['cal_date'])
    else:
        lastday=list(mgtodf(mftb.find({'trade_date':{'$gte':'20200301'}}))['trade_date'].sort_values())[-1]
        tradedays = trade_days(start_date=lastday, end_date=latestday())[1:]
    jishu=0
    for i in tradedays:
        df2=pro.block_trade(trade_date=i)
        jishu+=1
        print(i,'block_trade',len(df2.index))
        if df2.size>0:
            rec = json.loads(df2.T.to_json()).values()
            nums = mftb.insert_many(rec)
        else:print(i,'data not update')
        if jishu>=198:
            print('mid waiting...(60s)')
            time.sleep(60)
            jishu=0
    con.close
